package com.script

import java.io.OutputStream
import java.io.PrintWriter
import javax.script.SimpleScriptContext

class ScriptContextImpl : SimpleScriptContext() {
    private val buffer = StringBuilder()
    init {

        writer = PrintWriter( object : OutputStream() {
            override fun write(b: Int) {
                if (b == "\\n".toInt()) {
                    val line = buffer.toString()
//                    interceptedLines.add(line)
                    buffer.clear()
                } else {
                    buffer.append(b.toChar())
                }
            }

            override fun write(b: ByteArray?, off: Int, len: Int) {
                super.write(b, off, len)
            }

        }, true)

        errorWriter = PrintWriter( object : OutputStream() {
            override fun write(b: Int) {
                if (b == "\\n".toInt()) {
                    val line = buffer.toString()
//                    interceptedLines.add(line)
                    buffer.clear()
                } else {
                    buffer.append(b.toChar())
                }
            }
        }, true)


    }

}